package com.reliable.service.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reliable.api.param.IdParam;
import com.reliable.api.param.session.ReliableAnalyzedSessionsParam;
import com.reliable.common.core.domain.AjaxResult;
import com.reliable.common.core.page.TableDataInfo;
import com.reliable.common.utils.StringUtils;
import com.reliable.service.domain.ReliableAnalyzedSession;
import com.reliable.service.domain.ReliableTransactionRecord;
import com.reliable.api.dto.ReliableAnalyzedSession.ReliableAnalyzedSessionDTO;
import com.reliable.service.service.IReliableAnalyzedSessionService;
import com.reliable.service.service.IReliableTransactionRecordService;
import com.reliable.service.utils.IdEncryptUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reliable/analyzed")
@Api(value = "会话列表", tags = {"会话列表管理"})
public class ReliableAnalyzedSessionsController {
    @Autowired
    private IReliableAnalyzedSessionService reliableAnalyzedSessionService;

    @Autowired
    private IReliableTransactionRecordService reliableTransactionRecordService;


    /**
     * 会话列表查询
     */
    @ApiOperation("会话列表查询")
    @PostMapping("/sessionList")
    public AjaxResult list (@RequestBody ReliableAnalyzedSessionsParam param) {
//        ReliableUserLite user = JwtUtils.getCurrentUser();
//        Long id = user.getId();
        LambdaQueryWrapper<ReliableAnalyzedSession> wrapper = new LambdaQueryWrapper<>();
        // create_time时间范围查询
        wrapper.ge(param.getBeginTime() != null, ReliableAnalyzedSession::getCreateTime, param.getBeginTime());
        wrapper.le(param.getEndTime() != null, ReliableAnalyzedSession::getCreateTime, param.getEndTime());

        if (StringUtils.isNotEmpty(param.getTaskId()))
        {
            wrapper.eq(ReliableAnalyzedSession::getTaskId, IdEncryptUtils.decrypt(param.getTaskId()));
        }
        wrapper.eq(StringUtils.isNotEmpty(param.getRiskLevel()), ReliableAnalyzedSession::getRiskLevel, param.getRiskLevel());
        IPage<ReliableAnalyzedSession> page = reliableAnalyzedSessionService.page(new Page<>(param.getPageNum(), param.getPageSize()),wrapper);
        List<ReliableAnalyzedSession> records = page.getRecords();
        List<ReliableAnalyzedSessionDTO> list = new ArrayList<>();
        records.forEach(task -> {
            ReliableAnalyzedSessionDTO reliableAnalyzedSessionVO = new ReliableAnalyzedSessionDTO();
            BeanUtils.copyProperties(task, reliableAnalyzedSessionVO);
            reliableAnalyzedSessionVO.setSessionId(task.getSessionId());
            list.add(reliableAnalyzedSessionVO);
        });
        TableDataInfo data = new TableDataInfo(list, (int) page.getTotal());
        return AjaxResult.success(data);
    }

    /**
     * 会话详情
     */
    @ApiOperation("会话详情")
    @PostMapping("/sessionInfo")
    public AjaxResult info (@RequestBody IdParam param) {
        if (param.getBusinessId()==null)  return AjaxResult.error("请传入有效参数");
        LambdaQueryWrapper<ReliableAnalyzedSession> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ReliableAnalyzedSession::getSessionId, param.getBusinessId());
        ReliableAnalyzedSession one = reliableAnalyzedSessionService.getOne(wrapper);
        if (one == null) {
            return AjaxResult.error("未找到对应的会话信息");
        }
        one.setDelFlag(null);
        return AjaxResult.success(one);
    }

    @ApiOperation("会话总数量")
    @PostMapping("/getCount")
    public AjaxResult getCount () {
        long count = reliableAnalyzedSessionService.count();
        
        // 计算今天的数量
        LocalDateTime todayStart = LocalDate.now().atStartOfDay();
        LocalDateTime todayEnd = LocalDate.now().atTime(LocalTime.MAX);
        LambdaQueryWrapper<ReliableAnalyzedSession> todayWrapper = new LambdaQueryWrapper<>();
        todayWrapper.between(ReliableAnalyzedSession::getCreateTime, todayStart, todayEnd);
        long todayCount = reliableAnalyzedSessionService.count(todayWrapper);
        
        // 计算昨天的数量
        LocalDateTime yesterdayStart = LocalDate.now().minusDays(1).atStartOfDay();
        LocalDateTime yesterdayEnd = LocalDate.now().minusDays(1).atTime(LocalTime.MAX);
        LambdaQueryWrapper<ReliableAnalyzedSession> yesterdayWrapper = new LambdaQueryWrapper<>();
        yesterdayWrapper.between(ReliableAnalyzedSession::getCreateTime, yesterdayStart, yesterdayEnd);
        long yesterdayCount = reliableAnalyzedSessionService.count(yesterdayWrapper);
        
        Map<String, Object> result = new HashMap<>();
        result.put("totalCount", count);
        result.put("todayCount", todayCount);
        result.put("yesterdayCount", yesterdayCount);
        
        return AjaxResult.success(result);
    }

}
